﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Control_Robotic
{
    public partial class ControlRobotCar : Form
    {
        Color Current_corlor;
        public ControlRobotCar()
        {
            InitializeComponent();
        }
        private void addTocombobox()
        {
            Current_corlor = gostraing_bt.BackColor;
            for (int i = 1; i <= 20; i++) COM_cbb.Items.Add("COM" + i.ToString());
            for (int i = 600; i < 120000; i = i * 2) Baurate_cbb.Items.Add(i.ToString());

        }
        private void ControlRobotCar_Load(object sender, EventArgs e)
        {
            addTocombobox();
        }

        private void conn_bt_Click(object sender, EventArgs e)
        {
            try
            {
                if (P1.IsOpen) P1.Close();
                P1.PortName = COM_cbb.Text.ToString();
                P1.BaudRate = Convert.ToInt32(Baurate_cbb.Text.ToString());
                P1.Open();
                if (P1.IsOpen) MessageBox.Show("Robot Car is Ready!");
            }
            catch{
                MessageBox.Show("Select other COM Port, Please!");
            };
        }

        private void P1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {

        }

        private void disconn_bt_Click(object sender, EventArgs e)
        {
            if (P1.IsOpen) P1.Close();
            MessageBox.Show("Disconnected!");
        }

        private void gostraing_bt_MouseEnter(object sender, EventArgs e)
        {
            gostraing_bt.BackColor = Color.Blue;
            if(P1.IsOpen) P1.Write("w");
        }
        private void gostraing_bt_MouseLeave(object sender, EventArgs e)
        {
            gostraing_bt.BackColor = Current_corlor;
        }

        private void right_bt_MouseEnter(object sender, EventArgs e)
        {
            right_bt.BackColor = Color.Blue;
            if (P1.IsOpen) P1.Write("e");
        }

        private void right_bt_MouseLeave(object sender, EventArgs e)
        {
            right_bt.BackColor = Current_corlor;
        }

        private void left_bt_MouseEnter(object sender, EventArgs e)
        {
            left_bt.BackColor = Color.Blue;
            if (P1.IsOpen) P1.Write("q");
        }

        private void left_bt_MouseLeave(object sender, EventArgs e)
        {
            left_bt.BackColor = Current_corlor;
        }

        private void stop_bt_MouseEnter(object sender, EventArgs e)
        {
            stop_bt.BackColor = Color.Red;
            if (P1.IsOpen) P1.Write("p");
        }

        private void stop_bt_MouseLeave(object sender, EventArgs e)
        {
            stop_bt.BackColor = Current_corlor;
        }

        private void leftback_bt_MouseEnter(object sender, EventArgs e)
        {
            leftback_bt.BackColor = Color.Blue;
            if (P1.IsOpen) P1.Write("z");
        }

        private void leftback_bt_MouseLeave(object sender, EventArgs e)
        {
            leftback_bt.BackColor = Current_corlor;
        }

        private void goback_bt_MouseEnter(object sender, EventArgs e)
        {
            goback_bt.BackColor = Color.Blue;
            if (P1.IsOpen) P1.Write("s");
        }

        private void goback_bt_MouseLeave(object sender, EventArgs e)
        {
            goback_bt.BackColor = Current_corlor;
        }

        private void rightback_bt_MouseEnter(object sender, EventArgs e)
        {
            rightback_bt.BackColor = Color.Blue;
            if (P1.IsOpen) P1.Write("c");
        }

        private void rightback_bt_MouseLeave(object sender, EventArgs e)
        {
            rightback_bt.BackColor = Current_corlor;
        }

        private void gostraing_bt_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

       
    }
}
